/**
 * ExtGames.MainWindow
 * @extends Ext.Window
 * Renders a Solitaire game
 */
ExtGames.MainWindow = function(config) {  
  this.game = config.game;
  
  var opp = util.getOpponents();
    
  var comps = [];
  
  var hands = this.game.createHands();
  var addComp = this.game.getAddHandComp();
  
  for (var i = 0; i < opp.length; i++) {
	  comps.push(hands[i+1]);
	  if (addComp.length > 0) {
		  for (var j = 0; j < addComp[i+1].length; j++) {
			  comps.push(addComp[i+1][j]);
		  }
	  }
  }
      
  this.otherPlayerPanel = new Ext.Panel({
	    layout: 'column',
	    region: 'north',
	    height: 125,
	    cmargins: '5 0 0 0'	,
		html: '',
		items: comps
  });
  
  comps = [];
  
  var decks = this.game.createDecks();
  for (var i = 0; i < decks.length; i++) {
	  comps.push(decks[i]);
  }
  var discardPiles = this.game.createDiscardPiles();
  for (var i = 0; i < discardPiles.length; i++) {
	  comps.push(discardPiles[i]);
  }
    
  comps.push(hands[0]);
  if (addComp.length > 0) {
	  for (var i = 0; i < addComp[0].length; i++) {
		  comps.push(addComp[0][i]);
	  }
  }
    
  this.bottomPanel = new Ext.Panel({
	  id: 'my-panel',
	  layout: 'column',
	    region: 'center',
	    height: 125,
	    cls: 'bottom-table',
	    cmargins: '5 0 0 0',
	    defaults: {
	        border: false
	      },
  		items: comps
  });
    
  this.rightPanel = this.game.createScorecard();
  
  this.mainPanel = new Ext.Panel({
	  layout: 'border',
	  region: 'center',
	  defaults: {
	        border: false
	      },
	  items: [
		  //this.decksPanel,
		  this.bottomPanel,
		  this.otherPlayerPanel
		  ]
  });
      
  this.bbar = new Ext.StatusBar({
	id: 'status_bar',
    statusAlign: 'left',
    text: ''
  });
 
 
  var tbarItems = [];
  tbarItems.push({
	text:    'New Game',
    scope:   this,
    handler: function() {
    	this.game.newGameClicked();
    }
  });
 
 
	tbarItems.push({
      	id: 'nextRoundButton',
      	text: 'Next Round',
      	scope: this,
      	disabled: true,
      	handler: function() {
      		util.submitNextRound();
        	Ext.getCmp('nextRoundButton').disable();
      	}
    });
    
    tbarItems.push({
      	id: 'autoSortButton',
      	text: 'Auto-Sort',
      	enableToggle: true,
      	allowDepress: true,
      	pressed: true,
      	scope: this,
      	toggleHandler: function(button, state) {
      		hands[0].toggleAutoSort(state);
      	}
    });
    
    tbarItems.push('-');
    
    var extraButtons = config.game.getToolbarButtons();
    for (var i = 0; i < extraButtons.length; i++) {
    	tbarItems.push(extraButtons[i]);
    }
		
 
  this.tbar = new Ext.Toolbar({
  	id: 'tool_bar',
  	items: tbarItems
  });

  this.panel = new Ext.Panel({
    title:     'ExtGames',
    layout:'border',
    
    tbar: this.tbar,
    
    bbar: this.bbar,
    
    defaults: {
      border: false
    },
    
    items: [
      this.mainPanel,
      this.rightPanel
    ]
  });
  
  this.removeComponents =  function() {
  	while (this.otherPlayerPanel.items.length > 0) {
 		this.otherPlayerPanel.remove(this.otherPlayerPanel.items.last());
  	  }
  	  
  	  while (this.bottomPanel.items.length > 0) {
 		this.bottomPanel.remove(this.bottomPanel.items.last());
  	  }
  };
  
  this.redraw = function() {
  	  
	  var decks = this.game.createDecks();
	  for (var i = 0; i < decks.length; i++) {
		  this.bottomPanel.add(decks[i]);
	  }
	  var discardPiles = this.game.createDiscardPiles();
	  for (var i = 0; i < discardPiles.length; i++) {
		  this.bottomPanel.add(discardPiles[i]);
	  }
  	  
	  var hands = this.game.createHands();
	  var addComp = this.game.getAddHandComp();
	  
	  this.bottomPanel.add(hands[0]);
	  if (addComp.length > 0) {
		  for (var i = 0; i < addComp[0].length; i++) {
			  this.bottomPanel.add(addComp[0][i]);
		  }
	  }
	  
	  for (var i = 0; i < opp.length; i++) {
		  this.otherPlayerPanel.add(hands[i+1]);
		  if (addComp.length > 0) {
			  for (var j = 0; j < addComp[i+1].length; j++) {
				  this.otherPlayerPanel.add(addComp[i+1][j]);
			  }
		  }
	  }
	  
	  this.panel.doLayout();
  	  
  };
  
  this.updateStatus = function(status) {
	  Ext.getCmp('status_bar').setText(status);
  };
  
  this.render = function() {
  	this.viewport = new Ext.Viewport({
  		
		cls: 'background-green',
  		layout: 'fit',
  		items: [this.panel]

  	});
  }
  
}